package cn.maxhou.controller;

import cn.maxhou.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;


@Controller
public class HelloController6 {
    private static final Logger LOGGER= LoggerFactory.getLogger(HelloController6.class);

    @RequestMapping("/hello6")
    protected ModelAndView hello3(@RequestParam(value="realName",required = false) String name) {
        LOGGER.info("你好{},欢迎学习SpringMVC!",name);
        ModelAndView modelAndView=new ModelAndView();
        //添加对象到Model模型数据中,类似于request.setAttribute()方法
        modelAndView.addObject("realName",name);
        //设置逻辑视图名
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @RequestMapping("/model")
    public String model(Model model,@RequestParam(value="realName",required = false) String name) {
        LOGGER.info("你好{},在参数注入Model对象也可以出参",name);
        //使用model对象添加数据
        model.addAttribute("realName",name);
        return "hello";
    }

    @RequestMapping("/model2")
    public String model2(Model model,@RequestParam(value="realName",required = false) String name) {
        LOGGER.info("你好{},在参数注入Model对象也可以出参",name);
        //使用model对象添加数据
        model.addAttribute("realName",name);
        //创建SysUser对象,并存入model对象
        SysUser sysUser=new SysUser();
        sysUser.setRealName(name);
        model.addAttribute("sysUser",sysUser);
        return "hello";
    }

    @RequestMapping("/map")
    public String model2(Map map, @RequestParam(value="realName",required = false) String name) {
        LOGGER.info("你好{},在参数注入Map对象也可以出参",name);
        //使用model对象添加数据
        map.put("realName",name);
        //创建SysUser对象,并存入model对象
        SysUser sysUser=new SysUser();
        sysUser.setRealName(name);
        map.put("sysUser",sysUser);
        return "hello";
    }
}
